﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouristAgencyDataLayer;

namespace TouristAgencyWinApp
{
    public partial class HotelFacilitiesForm : Form
    {
        private HotelFacility selectedFacility; 

        public HotelFacilitiesForm()
        {
            InitializeComponent();
            WinLoad();
        }

        private void WinLoad()
        {
            hotelFacilityBindingSource.DataSource = HotelFacilities.GetHotelFacilities();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (selectedFacility != null && !string.IsNullOrEmpty(this.hfTxt.Text))
            {
                HotelFacilities.UpdateFacility(selectedFacility.HotelFacilityId, this.hfTxt.Text);
                MessageBox.Show("Changes have been saved");
            }
            else if (!string.IsNullOrEmpty(this.hfTxt.Text))
            {
                HotelFacilities.CreateHotelFacility(this.hfTxt.Text);
                MessageBox.Show("New Hotel Facilities has been added");
            }
            WinLoad();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != this.hotelFDG.Rows.Count - 1)
            {
                selectedFacility = HotelFacilities.GetHotelFacilityById(((HotelFacility)this.hotelFDG.Rows[e.RowIndex].DataBoundItem).HotelFacilityId);
                this.hfTxt.Text = selectedFacility.HotelFacility1;
            }
            else
            {
                this.hfTxt.Text = "";
                selectedFacility = null;
            }
        }

        private void hotelFDG_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (selectedFacility != null)
            {
                HotelFacilities.DeleteFacility(selectedFacility.HotelFacilityId);
                MessageBox.Show("The selected item has been deleted");
        }
            else
            {
                MessageBox.Show("Select some item first.");
            }
            WinLoad();
        }
    }
}
